import React from 'react';
import Util from '../../common/util.js';
import GoodsInfo from './goods-info.js';
import $ from 'webpack-zepto';
import Config from '../../common/config.js';

var scope;

export default class OrderDetail extends React.Component{
    constructor(props){
        super(props);
        document.title = '订单详情'
        scope = this;
        var order = scope.getOrder();
        var traces = scope.getTraces(order.order_sn);
        scope.state = {
            order : order,
            traces : traces
        };
    };

    getOrder(){
        var order = Util.storage().get('orderDetail');
        if(!order){
            window.location.href = 'order.html?type=order&appid=' + Util.getUrlParam('appid');
            return;
        }
        return order;
    };

    render(){
        return (
            <div className="orderDetail">
                {scope.orderInfo()}
                {scope.goodsInfo()}
                {scope.logisticsInfo()}
            </div>
        );
    };

    orderInfo(){
        var order = scope.state.order;
        return (
            <div className="orderInfo">
                {scope.orderInfoItem('订单状态',order.state)}
                {scope.orderInfoItem('订单编号',order.order_sn)}
                {scope.orderInfoItem('下单时间',order.create_time)}
                {scope.orderInfoItem('联系方式',order.contact + '    ' + order.tel)}
                {scope.orderInfoItem('收货地址',order.ship_province + order.ship_city + order.ship_district + order.ship_address)}
                {scope.orderInfoItem('买家留言',order.remark)}
                <div className="line"></div>
            </div>
        );
    };

    orderInfoItem(title,text){
        return (
            <div>
                <span>{title}:</span><span>{text}</span>
            </div>
        );
    };

    goodsInfo(){
        let goods = {orderGoods : scope.state.order.goods};
        let total = 0,list = [], activityList = [];

        goods.orderGoods.forEach(function(obj){
            total += parseInt(obj.sale_price) * parseInt(obj.goods_num);

            if(obj.act_name && obj.act_name != ''){
                activityList.push({
                    gc_thumb : obj.goods_thumb,
                    goods_name : obj.goods_name,
                    gc_name : obj.gc_name,
                    spec_desc : obj.spec_desc,
                    add_price : obj.sale_price,
                    yhg_goods_num : obj.goods_num,
                    goods_pid : obj.goods_pid
                });
            }else{
                list.push(obj);
            }
        });

        list.forEach(function(goods){
            goods.yhg_goods = [];
            activityList.forEach(function(activity){
                if(activity.goods_pid == goods.goods_id){
                    goods.yhg_goods.push(activity);
                }
            });
        });
        goods.orderGoods = list;
        return (
            <div className="pay goodsInfo">
                <GoodsInfo data={goods}></GoodsInfo>
                <div className="footer">
                    总金额 : RMB <label>{total}</label>
                </div>
            </div>
        );
    };

    logisticsInfo(){
        return (
            <div className="logistics">
                <div className="title">物流跟踪</div>
                {scope.state.traces.length == 0 ? scope.emptyTraces() : scope.traces()}
            </div>
        );
    };

    emptyTraces(){
        return (
            <div className="empty">
                该订单暂无物流信息
            </div>
        );
    };

    traces(){
        var traces = scope.state.traces;
        return (
            <div className="traces">
                {traces.map(function(obj,i){
                    return (
                        <div className="trace" key={i}>
                            <div className="point"></div>
                            <div className="dian">
                                <div className="text">
                                    <div>{obj.AcceptStation}</div>
                                    <div>{obj.AcceptTime}</div>
                                </div>
                            </div>
                        </div>);
                })}
            </div>
        );
    };

    getTraces(orderSn) {
        var traces = [];
        $.ajax({
            url : Config.path('duolu/fast',['?uToken=' + Util.storage().get(scope.props.appId),'&order_sn=' + orderSn]),
            type : 'get',
            async : false,
            success : function(resp){
                if(resp.status == 200){
                    traces = resp.data.Traces;
                }
            }
        });

        return traces;
    };
};
